/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Admin_InventoryReport.java
 *
 * Created on Apr 7, 2011, 6:07:12 PM
 */
package admin_gui;

import admin.BusinessLogic.BusinessLogic;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author edaype
 */
public class Admin_InventoryReport extends javax.swing.JPanel implements Printable  {

    /** Creates new form Admin_InventoryReport */
  //  private Vector storeName;
    private Vector v_head;
    private String[] sname;
    private BusinessLogic businessLogic;

    public Admin_InventoryReport(JPanel panel, Vector storeName) {
        initComponents();
 //       this.storeName = storeName;
        sname = new String[storeName.size()];
        for (int i = 0; i < storeName.size(); i++) {
            sname[i] = ((Vector) storeName.elementAt(i)).get(0).toString();
        }
        storeNameBox.setModel(new javax.swing.DefaultComboBoxModel(sname));
        v_head = new Vector();
        v_head.add("UPC");       // No store ID?
        v_head.add("Item Name");
        v_head.add("Actual Quantity");
        v_head.add("Average");
        try {
            businessLogic = new BusinessLogic();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Admin_InventoryReport.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Admin_InventoryReport.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TitlePanel = new javax.swing.JPanel();
        InventoryPane = new javax.swing.JScrollPane();
        InventoryTable = new javax.swing.JTable();
        SearchLabel = new javax.swing.JLabel();
        GoButton = new javax.swing.JButton();
        storeNameBox = new javax.swing.JComboBox();
        TitleLabel = new javax.swing.JLabel();
        print = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        TitlePanel.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout TitlePanelLayout = new javax.swing.GroupLayout(TitlePanel);
        TitlePanel.setLayout(TitlePanelLayout);
        TitlePanelLayout.setHorizontalGroup(
            TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 116, Short.MAX_VALUE)
        );
        TitlePanelLayout.setVerticalGroup(
            TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 69, Short.MAX_VALUE)
        );

        InventoryTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "UPC", "Name", "Actual Quantity", "Required"
            }
        ));
        InventoryPane.setViewportView(InventoryTable);

        SearchLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        SearchLabel.setText("Store Name");

        GoButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        GoButton.setText("Go");
        GoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GoButtonActionPerformed(evt);
            }
        });

        storeNameBox.setFont(new java.awt.Font("Tahoma", 0, 24));
        storeNameBox.setName("nameField"); // NOI18N

        TitleLabel.setFont(new java.awt.Font("Copperplate Gothic Bold", 2, 43));
        TitleLabel.setText("Inventory Report (Store)");

        print.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        print.setText("Print");
        print.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                printActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(InventoryPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 735, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(TitleLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(TitlePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(SearchLabel)
                        .addGap(37, 37, 37)
                        .addComponent(storeNameBox, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(84, 84, 84)
                        .addComponent(GoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(print, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(storeNameBox, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(SearchLabel))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(GoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(print, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(InventoryPane, javax.swing.GroupLayout.PREFERRED_SIZE, 483, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(TitlePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(TitleLabel))))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void GoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GoButtonActionPerformed
        String s = storeNameBox.getSelectedItem().toString();
        DefaultTableModel tm = new DefaultTableModel(businessLogic.getInventoryReport(
                s), v_head);
        InventoryTable.setModel(tm);
        ((DefaultTableModel) InventoryTable.getModel()).fireTableStructureChanged();
}//GEN-LAST:event_GoButtonActionPerformed

    private void printActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printActionPerformed
        PrinterJob pj = PrinterJob.getPrinterJob();
                pj.setPrintable(Admin_InventoryReport.this);
                pj.printDialog();
                try {
                    pj.print();
                } catch (Exception PrintException) {
                }
    }//GEN-LAST:event_printActionPerformed
    public int print(Graphics g, PageFormat pageFormat,
            int pageIndex) throws PrinterException {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.black);
        int fontHeight = g2.getFontMetrics().getHeight();
        int fontDesent = g2.getFontMetrics().getDescent();

        //leave room for page number
        double pageHeight =
                pageFormat.getImageableHeight() - fontHeight;
        double pageWidth =
                pageFormat.getImageableWidth();
        double tableWidth = (double) InventoryTable.getColumnModel().
                getTotalColumnWidth();
        double scale = 1;
        if (tableWidth >= pageWidth) {
            scale = pageWidth / tableWidth;
        }

        double headerHeightOnPage =
                InventoryTable.getTableHeader().getHeight() * scale;
        double tableWidthOnPage = tableWidth * scale;

        double oneRowHeight = (InventoryTable.getRowHeight() +
                InventoryTable.getRowMargin()) * scale;
        int numRowsOnAPage =
                (int) ((pageHeight - headerHeightOnPage) /
                oneRowHeight);
        double pageHeightForTable = oneRowHeight *
                numRowsOnAPage;
        int totalNumPages =
                (int) Math.ceil(((double) InventoryTable.getRowCount()) /
                numRowsOnAPage);
        if (pageIndex >= totalNumPages) {
            return NO_SUCH_PAGE;
        }

        g2.translate(pageFormat.getImageableX(),
                pageFormat.getImageableY());
//bottom center
        g2.drawString("Page: " + (pageIndex + 1),
                (int) pageWidth / 2-35, (int) (pageHeight + fontHeight -
                fontDesent));

        g2.translate(0f, headerHeightOnPage);
        g2.translate(0f, -pageIndex * pageHeightForTable);

        //If this piece of the table is smaller
        //than the size available,
        //clip to the appropriate bounds.
        if (pageIndex + 1 == totalNumPages) {
            int lastRowPrinted =
                    numRowsOnAPage * pageIndex;
            int numRowsLeft =
                    InventoryTable.getRowCount() - lastRowPrinted;
            g2.setClip(0,
                    (int) (pageHeightForTable * pageIndex),
                    (int) Math.ceil(tableWidthOnPage),
                    (int) Math.ceil(oneRowHeight *
                    numRowsLeft));
        } //else clip to the entire area available.
        else {
            g2.setClip(0,
                    (int) (pageHeightForTable * pageIndex),
                    (int) Math.ceil(tableWidthOnPage),
                    (int) Math.ceil(pageHeightForTable));
        }

        g2.scale(scale, scale);
        InventoryTable.paint(g2);
        g2.scale(1 / scale, 1 / scale);
        g2.translate(0f, pageIndex * pageHeightForTable);
        g2.translate(0f, -headerHeightOnPage);
        g2.setClip(0, 0,
                (int) Math.ceil(tableWidthOnPage),
                (int) Math.ceil(headerHeightOnPage));
        g2.scale(scale, scale);
        InventoryTable.getTableHeader().paint(g2);
        //paint header at top

        return Printable.PAGE_EXISTS;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton GoButton;
    private javax.swing.JScrollPane InventoryPane;
    private javax.swing.JTable InventoryTable;
    private javax.swing.JLabel SearchLabel;
    private javax.swing.JLabel TitleLabel;
    private javax.swing.JPanel TitlePanel;
    private javax.swing.JButton print;
    private javax.swing.JComboBox storeNameBox;
    // End of variables declaration//GEN-END:variables
}
